package solitaire.deck;

import java.awt.Graphics;
import java.awt.geom.Point2D;
import java.util.ArrayList;

public class PileCollection extends ArrayList<Pile> {

	public Pile find( Point2D point )
    {
		Pile result = null;
        for( Pile obj : this )
        {
            if( obj.contains( point ) ) result = obj;
        }
        
        //look through cards
        Card card = findCard( point );
        if( card != null )
        {
        	return card.getPile();
        }
     
        return result;
    }
	
	public Card findCard( Point2D point )
    {
		Card result = null;
      
		if(this != null) {
	        for( Pile obj : this ) {
	            for( Card card : obj.getCards())
	            {
	            	if( card.contains( point ) ) result = card;
	            }
	        }
        }
        return result;
    }
	
	public Card findCardUnderThisOne( Point2D point, Card currentCard )
    {
		Card result = null;
      
		if(this != null) {
	        for( Pile obj : this ) {
	            for( Card card : obj.getCards())
	            {
	            	if( currentCard != card && card.contains( point ) )
	            	{
	            		result = card;
	            	}
	            }
	        }
        }
        return result;
    }
	
	public void removeAllCards()
	{
		if(this != null) {
	        for( Pile obj : this ) {
	        	obj.getCards().clear();
	        }
		}
	}
	
	public void paintComponent( Graphics g ) 
	{
		if(this != null) {
	        for( Pile obj : this ) {
	            obj.drawObject( g );
	            
	            for( Card card : obj.getCards())
	            {
	            	if(card != null)
	            	{
	            		card.drawObject(g);
	            	}
	            }
	        }
		}
    }
}
